﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Application.Admin.Dto.PushLogDto>
@{
    ViewBag.subtitle = "推送推送内容管理";
    ViewBag.title = "推送推送内容管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{Html.Telerik().Grid(Model)
    .Name("SightSiginGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "searchpushlog"))
        {
        <label>
            推送结果:</label>
        <select id="resultType" name="resultType">
            <option value="0">--请选择--</option>
            <option value="1">成功</option>
            <option value="2">失败</option>
            <option value="3">错误客户端</option>
            <option value="4">内容太长</option>
            <option value="5">错误</option>
            <option value="6">未知错误</option>
        </select>
        <label class="customer-label" for="Customers-input">
            推送内容:
        </label>
        <input type="text" id="searchPushLog" value="推送内容" data-autocomplete="/push/searchpushlog" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' style="margin-left: 10px;" />
        }
        if (Html.HasUiPermission(this.ViewContext, "muldeletepushlog"))
        { 
        <a href='javascript:;' class='t-button' onclick='mulDelete()' style="margin-left: 5px;">
            删除选择项</a> 
        }
        if (Html.HasUiPermission(this.ViewContext, "clearallpushlog"))
        { 
        <a id="clearPushLog" href="javascript:;" class="t-button" style="float: right;">清空推送日志</a> 
        }
        if (Html.HasUiPermission(this.ViewContext, ""))
        {
        <a href='javascript:;' class='t-button' style="float: right; display: none;">导出上次失败记录</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();
        columns.Bound(o => o.ClientName).Width(200).Title("客户端名");
        columns.Bound(o => o.ClientId).Width(200).Title("客户端Id").Hidden();
        columns.Bound(o => o.ResultType).Width(200).Title("推送结果");
        columns.Bound(o => o.Result).Width(200).Title("结果").Hidden();
        columns.Bound(o => o.Content).Width(200).Title("推送内容");
        columns.Bound(o => o.ContentId).Width(200).Title("内容Id").Hidden();

        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").ReadOnly();
        columns.Bound(o => o.AddTime).Format("{0:yyyy-MM-dd HH:mm:ss}").Width(200).Title("添加时间").ReadOnly();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();

        columns.Template(@<text><a href = 'javascript:;'  class = 't-button customDelete' rel = '@(item.Id)'>删除</a></text>)
        .ClientTemplate("<a href = 'javascript:;'  class = 't-button customDelete' rel = '<#=Id #>'>删除</a>")
        .Title("删除").Hidden(!Html.HasUiPermission(this.ViewContext, "deletepushlog"));
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding")
        .OnDataBound("onCustomDelete").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxpushlogindex", "push")
        .Delete("deletepushlog", "push"))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#SightSiginGrid").data("tGrid").rebind();
    }

    function onCustomDelete() {
        $('.customDelete').bind('click', function () {
            var id = $(this).attr('rel');
            alert(id);
            var data = { id: id };
            $.post('/push/deletepushlog', data, function (res) {
                if (res.success) {
                    $("#SightSiginGrid").data("tGrid").rebind();
                } else {
                    alert(res.message);
                }
            });
        });
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchPushLog").val();
        var resultType = $('#resultType').val();
        preSearch = resultType;
        if (searchKeyStr == '推送内容') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr, resultType: resultType });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/push/muldeletepushlog', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#SightSiginGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindComplete(e) {
        $("input:checkbox").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox").attr("checked", false);
        onCustomDelete();
        $('#checkAllRecords').click(function checkAll() { $("#SightSiginGrid tbody input:checkbox").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: function (request, response) {
                    var url = '/push/searchpushlog/';
                    var resultType = $('#resultType').val();
                    $.getJSON(url, {
                        term: $.trim(request.term),
                        resultType: resultType
                    }, response);
                }
            });
        });
        $("#searchPushLog").focus(function () {
            if (this.value == "推送内容") {
                $("#searchPushLog").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchPushLog").val("推送内容");
            }
        });

        $("#clearPushLog").click(function () {
            if (confirm("你确定要清空推送日志吗？")) {
                $.post('/push/clearallpushlog', {}, function (res) {
                    if (res.success) {
                        $("#SightSiginGrid").data("tGrid").rebind();
                    }
                    else {
                        alert(res.message);
                    }
                });
            }
        });
    });
</script>
